﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZK.InterfaceServer;

namespace ZK.YHP_TransManage.UC.HardWare
{
    public partial class AddUpdate : UserControl
    {
        int id = 0;
        Models.HardWareM hardWareM = new Models.HardWareM();
        public AddUpdate(string id = "0")
        {
            InitializeComponent();
            Init(id);
        }

        private void Init(string id)
        {
            this.id = Convert.ToInt32(id);
            if (this.id > 0)
            {
                Models.ReceiveDataBase<Models.HardWareM> receiveDataBase = AutoControlTransportApiServer.GetHardWareById(this.id.ToString());
                if (receiveDataBase != null && receiveDataBase.data != null)
                {
                    SetText(receiveDataBase.data);
                }
            }
        }
        void SetText(Models.HardWareM hardWareM)
        {
            this.hardWareM = hardWareM;
            //if (this.IsHandleCreated)
            //{
            //    this.Invoke(new Action(() =>
            //    {
            dateTimePickerusestime.Value = Convert.ToDateTime(hardWareM.usestime);
            textBoxName.Text = hardWareM.wareName;
            textBoxremark.Text = hardWareM.remark;
            if (hardWareM.iporserial == 1)
            {
                radioButtonip.Checked = true;
            }
            else
            {
                radioButtonserial.Checked = true;
            }
            textBoxportnum.Text = hardWareM.portnum.ToString();
            serialnametextBox.Text = hardWareM.serialname;
            if (hardWareM.isEnable == 1)
            {
                radioButtonenable1.Checked = true;
            }
            else
            {
                radioButtonenable0.Checked = true;
            }
            manufactortextBox.Text = hardWareM.manufactor;
            manufaturetimedateTimePicker.Value = Convert.ToDateTime(hardWareM.manufaturetime);
            textBoxip.Text = hardWareM.ip;
            //    }));
            //}
        }
        /// <summary>
        /// 取消按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button2_Click(object sender, EventArgs e)
        {
            ShowIndex();
        }

        private void ShowIndex()
        {
            var parentPanel = this.Parent.Parent.Controls;
            parentPanel[1].Controls.Clear();
            Index hardWare = new Index();
            hardWare.Parent = parentPanel[1];
            hardWare.Dock = DockStyle.Fill;
            hardWare.Show();
        }

        /// <summary>
        /// add update 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button1_Click(object sender, EventArgs e)
        {
            if (!Common.Utility.IsInt(textBoxportnum.Text.Trim()) || string.IsNullOrEmpty(textBoxportnum.Text.Trim()))
            {
                Common.MessageDialog.MessageBoxShow(Com.TipsStringHelper.dkhbxwsz, Com.TipsStringHelper.srgscw);
                return;
            }
            if (true)
            {

            }

            hardWareM.usestime = dateTimePickerusestime.Value.ToString("yyyy-MM-dd");
            hardWareM.wareName = textBoxName.Text.Trim();
            hardWareM.remark = textBoxremark.Text.Trim();
            hardWareM.iporserial = radioButtonip.Checked ? 1 : 0;
            hardWareM.portnum = Convert.ToInt32(textBoxportnum.Text.Trim());
            hardWareM.serialname = serialnametextBox.Text.Trim();
            hardWareM.isEnable = radioButtonenable1.Checked ? 1 : 0;
            hardWareM.manufactor = manufactortextBox.Text.Trim();
            hardWareM.manufaturetime = manufaturetimedateTimePicker.Value.ToString("yyyy-MM-dd");
            if (hardWareM.wareState < 1)
            {
                hardWareM.wareState = 0;
            }
            hardWareM.ip = textBoxip.Text.Trim();
            Models.ReceiveDataBase<string> receiveDataBase = new Models.ReceiveDataBase<string>();
            if (hardWareM.id > 0)
            {
                receiveDataBase = AutoControlTransportApiServer.UpdateHardWareByid(hardWareM);
            }
            else
            {
                receiveDataBase = AutoControlTransportApiServer.AddHardWare(hardWareM);
            }

            if (!receiveDataBase.success)
            {
                Common.MessageDialog.ShowLed(Com.TipsStringHelper.sbxxtjhgxsb, Com.TipsStringHelper.czsb);
                return;
            }
            Common.MessageDialog.MessageBoxShow(Com.TipsStringHelper.czcg, Com.TipsStringHelper.czcg);
            ShowIndex();
        }
    }
}
